
<html><HEAD>
<LINK REL=STYLESHEET HREF="default.css" TYPE="text/css">
<TITLE>
Exporting to XML</TITLE>
</HEAD>
<BODY>

<!-- Header -->
<p class="ancestor" align="right"><A HREF="pbugp249.htm">Previous</A>&nbsp;&nbsp;<A HREF="pbugp251.htm" >Next</A>
<!-- End Header -->
<A NAME="BABFDGAE"></A><h1>Exporting to XML</h1>
<A NAME="TI8659"></A><p>You can export the data in a DataWindow or DataStore object to
XML using any of the techniques used for exporting to other formats
such as PSR or HTML:<A NAME="TI8660"></A>
<ul>
<li class=fi>Using the <b>SaveAs</b> method:<p><PRE> ds1.SaveAs("C:\TEMP\Temp.xml", Xml!, true)</PRE></li>
<li class=ds>Using PowerScript dot notation or the <b>Describe</b> method:<p><PRE> ls_xmlstring = dw1.Object.DataWindow.Data.XML ls_xmlstring = dw1.Describe(DataWindow.Data.XML)</PRE></li>
<li class=ds>Using the Save Rows As menu item in the DataWindow painter.<br>
With the Preview view open, select File&gt;Save Rows
As, select XML from the Files of Type drop-down list, provide a
file name, and click Save. You can use this in the development environment
to preview the XML that will be generated at runtime.<br>
</li>
</ul>
</p>
<A NAME="TI8661"></A><p>When you export data, PowerBuilder uses an export template to
specify the content of the generated XML.</p>
<p><img src="images/note.gif" width=17 height=17 border=0 align="bottom" alt="Note"> <span class=shaded>Default export format</span> <A NAME="TI8662"></A>If you have not created or assigned an export template, PowerBuilder uses
a default export format. This is the same format used when you create
a new default export template. See <A HREF="pbugp248.htm#CHDBIFAA">"Creating templates"</A>. </p>
<A NAME="TI8663"></A>OLE DataWindow objects cannot be exported using a template. You must
use the default format.</p>
<A NAME="TI8664"></A><h2>Setting data export properties</h2>
<A NAME="TI8665"></A><p>The Data Export page in the Properties view lets you set properties
for exporting data to XML.</p>
<A NAME="TI8666"></A><p>In addition to the properties that you can set oin this page, PowerBuilder provides
two properties that you can use to let the user of an application
select an export template at runtime. See <A HREF="pbugp250.htm#CIHJJHCHK">"Selecting templates at
runtime"</A>.</p>
<A NAME="TI8667"></A><h3>The Use Template property</h3>
<A NAME="TI8668"></A><p>The names of all templates that you create and save for the
current DataWindow object display in the Use Template drop-down list. </p>
<br><img src="images/usetemp1.gif">
<A NAME="TI8669"></A><p>The template you select from the list is used to conform the
XMLgenerated by any of the methods for saving as XML to the specifications
defined in the named template. Selecting a template from the list
box sets the DataWindow object's Export.XML.UseTemplate property.
You can also modify the value of the UseTemplate property dynamically
in a script. For example, an XML publishing engine would change
templates dynamically to create different presentations of the same
data.</p>
<A NAME="TI8670"></A><p>When you open a DataWindow, the Export/Import Template
view displays the template specified in the DataWindow's
Use Template property. (If the view is not visible in the current
layout, select View&gt;Export/Import Template&gt;XML from
the menu bar.) If the property has not been set, the first saved
template displays or, if there are no saved templates, the default
structured template displays as a basis for editing. </p>
<A NAME="TI8671"></A><h4>Template used when saving</h4>
<A NAME="TI8672"></A><p>When the DataWindow is saved as XML, PowerBuilder uses the template specified
in the Use Template property. If the property has not been set, PowerBuilder uses
the default template.</p>
<A NAME="TI8673"></A><p>When you are working on a template, you might want to see
the result of your changes. The template specified in the Use Template
property might not be the template currently displayed in the Export/Import
Template view, so you should check the value of the Use Template
property to be sure you get the results you expect. </p>
<A NAME="TI8674"></A><p><img src="images/proc.gif" width=17 height=17 border=0 align="bottom" alt="Steps"> To save to XML using the current template:</p>
<ol><li class=fi><p>Right-click in the Export/Import
template view and select Save or Save As from the pop-up menu to
save the current template.</p></li>
<li class=ds><p>On the Data Export page in the properties view,
select the current template from the Use Template drop-down list.</p></li>
<li class=ds><p>Select File&gt;Save Rows As, select XML
from the Files of Type drop-down list, enter a file name, and click
Save.</p></li></ol>
<br><A NAME="CIHICCJF"></A><h3>Generating group headers</h3>
<A NAME="TI8675"></A><p>To generate the contents of the
header section iteratively for each group in a group DataWindow, check
the Iterate Header for Groups check box, or set the Export.XML.HeadGroups
DataWindow property. This property is on by default.</p>
<A NAME="TI8676"></A><p>For example, consider a group DataWindow object that includes the
columns <b>sales_order_id</b><b> </b>and<b> sales_order_order_date</b>.
The following screenshot shows the template for this DataWindow object:</p>
<br><img src="images/dwgrp01.gif">
<A NAME="TI8677"></A><p>The root element in the Header section of the template, Orders,
has a child element, Order. Order has an id attribute whose value
is a control reference to the column <b>sales_order_id</b>.
Order also has a child element, OrderDate, that contains a column
reference to the <b>sales_order_order_date</b> column.
These elements make up the header section that will be iterated
for each group.</p>
<A NAME="TI8678"></A><p>The Detail Start element, Item, has an id attribute whose
value is a control reference to the column <b>sales_order_items_line_id</b>.
It also has three child elements that contain column references
to the line items for product ID, quantity, and ship date.</p>
<A NAME="TI8679"></A><p>When the DataWindow is exported with the Export.XML.HeadGroups property
on, the order ID and date iterate for each group. The following
XML output shows the first three iterations of the group header:<p><PRE> &lt;?xml version="1.0" encoding="UTF-16LE" standalone="no"?&gt;<br>&lt;Orders&gt;<br>   &lt;Order id="2001"&gt;<br>      &lt;OrderDate&gt;2002-03-14&lt;/OrderDate&gt;<br>         &lt;Item id="1"&gt;<br>            &lt;Product&gt;300&lt;/Product&gt;<br>            &lt;Quantity&gt;12&lt;/Quantity&gt;<br>            &lt;ShipDate&gt;2005-09-15&lt;/ShipDate&gt;<br>         &lt;/Item&gt;<br>         &lt;Item id="2"&gt;<br>            &lt;Product&gt;301&lt;/Product&gt;<br>            &lt;Quantity&gt;12&lt;/Quantity&gt;<br>            &lt;ShipDate&gt;2005-09-14&lt;/ShipDate&gt;<br>         &lt;/Item&gt;<br>         &lt;Item id="3"&gt;<br>            &lt;Product&gt;302&lt;/Product&gt;<br>            &lt;Quantity&gt;12&lt;/Quantity&gt;<br>            &lt;ShipDate&gt;2005-09-14&lt;/ShipDate&gt;<br>         &lt;/Item&gt;<br>   &lt;/Order&gt;<br>   &lt;Order id="2002"&gt;<br>      &lt;OrderDate&gt;2002-03-18&lt;/OrderDate&gt;<br>         &lt;Item id="2"&gt;<br>            &lt;Product&gt;401&lt;/Product&gt;<br>            &lt;Qty&gt;24&lt;/Qty&gt;<br>            &lt;ShipDate&gt;2002-09-18&lt;/ShipDate&gt;<br>         &lt;/Item&gt;<br>         &lt;Item id="1"&gt;<br>            &lt;Product&gt;400&lt;/Product&gt;<br>            &lt;Qty&gt;24&lt;/Qty&gt;<br>            &lt;ShipDate&gt;2002-09-18&lt;/ShipDate&gt;<br>         &lt;/Item&gt;<br>   &lt;/Order&gt;<br>   &lt;Order id="2003"&gt;<br>      &lt;OrderDate&gt;2002-03-21&lt;/OrderDate&gt;<br>         &lt;Item id="3"&gt;<br>            &lt;Product&gt;400&lt;/Product&gt;<br>            &lt;Qty&gt;12&lt;/Qty&gt;<br>            &lt;ShipDate&gt;2002-09-23&lt;/ShipDate&gt;<br>         &lt;/Item&gt;<br>         ...</PRE></p>
<A NAME="TI8680"></A><p>For DataWindow objects with more than one group, when you generate
a new default template, each group after the first is identified
with a special icon and a check on the pop-up menu next to the Starts
Group Header item.</p>
<br><img src="images/xgrphdr.gif">
<A NAME="TI8681"></A><p>When the Iterate Header for Groups check box is selected",
each XML fragment in the header section between a Group Header element
and the next Group Header element or Detail Start element is iterated. </p>
<A NAME="TI8682"></A><p>In the template shown in the previous illustration, sales
are grouped by customer ID, then by order ID. The customer group
header has attributes for the customer's ID and first and
last names. The order group header has attributes for the order
ID and date. The following illustration shows the DataWindow in
the Design view:</p>
<br><img src="images/xgrphdr2.gif">
<A NAME="TI8683"></A><p>The following XML output shows the first iteration of the
customer group header and the first and second iterations of the
order group header:<p><PRE> &lt;?xml version="1.0" encoding="UTF-16LE" standalone="no"?&gt;<br>&lt;d_customer&gt;<br>   &lt;customer id="101" fname="Michaels" lname="Devlin"&gt;<br>      &lt;order id="2001" date="1996-03-14"&gt;<br>             &lt;order_item&gt;<br>            &lt;sales_order_items_line_id&gt;1&lt;/sales_order_items_line_id&gt;<br>            &lt;sales_order_items_prod_id&gt;300&lt;/sales_order_items_prod_id&gt;<br>            &lt;sales_order_items_quantity&gt;12&lt;/sales_order_items_quantity&gt;<br>         &lt;/order_item&gt;<br>         &lt;order_item&gt;<br>            &lt;sales_order_items_line_id&gt;2&lt;/sales_order_items_line_id&gt;<br>            &lt;sales_order_items_prod_id&gt;301&lt;/sales_order_items_prod_id&gt;<br>            &lt;sales_order_items_quantity&gt;12&lt;/sales_order_items_quantity&gt;<br>         &lt;/order_item&gt;<br>         &lt;order_item&gt;<br>            &lt;sales_order_items_line_id&gt;3&lt;/sales_order_items_line_id&gt;<br>            &lt;sales_order_items_prod_id&gt;302&lt;/sales_order_items_prod_id&gt;<br>            &lt;sales_order_items_quantity&gt;12&lt;/sales_order_items_quantity&gt;<br>         &lt;/order_item&gt;<br>      &lt;/order&gt;<br>      &lt;order id="2005" date="1996-03-24"&gt;<br>         &lt;order_item&gt;<br>            &lt;sales_order_items_line_id&gt;1&lt;/sales_order_items_line_id&gt;<br>            &lt;sales_order_items_prod_id&gt;700&lt;/sales_order_items_prod_id&gt;<br>            &lt;sales_order_items_quantity&gt;12&lt;/sales_order_items_quantity&gt;<br>         &lt;/order_item&gt;<br>      &lt;/order&gt;</PRE></p>
<A NAME="TI8684"></A><h3>Formatting the exported XML</h3>
<A NAME="TI8685"></A><p>By default, the XML is exported without formatting. If you
want to view or verify the exported XML in a text editor, check
the Include Whitespace check box or set the Export.XML.IncludeWhitespace
property in a script. Turning this property on causes the export
process to insert tabs, carriage returns, and linefeed characters
into the XML so that it is easier to read. Most of the examples
in this chapter were exported with this property turned on.</p>
<p><img src="images/note.gif" width=17 height=17 border=0 align="bottom" alt="Note"> <span class=shaded>Do not import formatted XML</span> <A NAME="TI8686"></A>You should not try to import XML formatted with white space
characters, because the white space between data element tags is
considered to be part of the element.</p>
<A NAME="CIHJICJE"></A><h3>Exporting metadata</h3>
<A NAME="TI8687"></A><p>You can specify
that metadata in the form of a DTD or schema should be exported
when you save the DataWindow object. You can choose to save the metadata
with the XML or in a separate file. </p>
<A NAME="TI8688"></A><p>If you export metadata as a schema, you can associate it with
a namespace. See <A HREF="pbugp250.htm#CIHHCBHD">"Associating a namespace
with an exported schema"</A>.</p>
<A NAME="TI8689"></A><p>To specify how metadata should be saved, select a value from
the Meta Data Type drop-down list or set the <b>Export.XML.MetaDataType</b> property.
The possible values are:<A NAME="TI8690"></A>
<ul>
<li class=fi>XMLNone!&#8212;No
metadata is generated</li>
<li class=ds>XMLSchema!&#8212;An XML schema is generated</li>
<li class=ds>XMLDTD!&#8212;A DTD is generated
</li>
</ul>
</p>
<A NAME="TI8691"></A><p>If the data item for a column is <b>null</b> or
an empty string, an empty element is created. If you select XMLSchema!,
child elements with <b>null</b> data items are created
with the content <FONT FACE="Courier New">"xsi:nil='true'"</FONT>.</p>
<A NAME="TI8692"></A><p>The metadata is saved into the exported XML itself or into
an associated file, depending on the setting in the SaveMeta Data
drop-down list or the <b>Export.XML.SaveMetaData</b> property.
The possible values are:<A NAME="TI8693"></A>
<ul>
<li class=fi>MetaDataInternal!&#8212;The
metadata is saved into the generated XML document or string. To
save metadata using the <b>.Data.XML</b> expression syntax,
you must use this value.</li>
<li class=ds>MetaDataExternal!&#8212;The metadata is saved
as an external file with the same name as the XML document but with
the extension <i>.xsd</i> (for a schema) or <i>.dtd</i> (for
a DTD). A reference to the name of the metadata file is included
in the output XML document. 
</li>
</ul>
</p>
<A NAME="TI8694"></A><h4>Example: internal metadata</h4>
<A NAME="TI8695"></A><p>For example, if you select XMLDTD! and MetaDataInternal!,
the header and first row of the exported XML would look like this
for a simple grid DataWindow for the <b>contact</b> table
in the <ACRONYM title = "E A S" >EAS</ACRONYM> Demo DB. The Include Whitespace
property has also been selected and the file name is <i>dtdinternal.xml</i>:<p><PRE> &lt;?xml version="1.0" encoding="UTF-16LE" standalone="yes"?&gt;<br>&lt;!DOCTYPE dtdinternal [&lt;!ELEMENT dtdinternal (dtdinternal_row*)&gt;<br>&lt;!ELEMENT dtdinternal_row (id, last_name, first_name, title, street, city, state, zip, phone, fax)&gt;<br>&lt;!ELEMENT id (#PCDATA)&gt;<br>&lt;!ELEMENT last_name (#PCDATA)&gt;<br>&lt;!ELEMENT first_name (#PCDATA)&gt;<br>&lt;!ELEMENT title (#PCDATA)&gt;<br>&lt;!ELEMENT street (#PCDATA)&gt;<br>&lt;!ELEMENT city (#PCDATA)&gt;<br>&lt;!ELEMENT state (#PCDATA)&gt;<br>&lt;!ELEMENT zip (#PCDATA)&gt;<br>&lt;!ELEMENT phone (#PCDATA)&gt;<br>&lt;!ELEMENT fax (#PCDATA)&gt;<br>]&gt;<br>&lt;dtdinternal&gt;<br>   &lt;dtdinternal_row&gt;<br>      &lt;id&gt;1&lt;/id&gt;<br>      &lt;last_name&gt;Hildebrand&lt;/last_name&gt;<br>      &lt;first_name&gt;Jane&lt;/first_name&gt;<br>      &lt;title&gt;ma&lt;/title&gt;<br>      &lt;street&gt;1280 Washington St.&lt;/street&gt;<br>      &lt;city&gt;Emeryville&lt;/city&gt;<br>      &lt;state&gt;MI&lt;/state&gt;<br>      &lt;zip&gt;94608&lt;/zip&gt;<br>      &lt;phone&gt;5105551309&lt;/phone&gt;<br>      &lt;fax&gt;5105554209&lt;/fax&gt;<br>   &lt;/dtdinternal_row&gt;</PRE></p>
<A NAME="TI8696"></A><h4>Example: external metadata</h4>
<A NAME="TI8697"></A><p>If you select MetaDataExternal! instead, the generated XML
in <i>dtdexternal.xml</i> looks like this:<p><PRE> &lt;?xml version="1.0" encoding="UTF-16LE"?&gt;<br>&lt;!DOCTYPE dtdexternal SYSTEM "dtdexternal.dtd"&gt;<br>&lt;dtdexternal&gt;<br>   &lt;dtdexternal_row&gt;<br>      &lt;id&gt;1&lt;/id&gt;<br>      &lt;last_name&gt;Hildebrand&lt;/last_name&gt;<br>      &lt;first_name&gt;Jane&lt;/first_name&gt;<br>      &lt;title&gt;ma&lt;/title&gt;<br>      &lt;street&gt;1280 Washington St.&lt;/street&gt;<br>      &lt;city&gt;Emeryville&lt;/city&gt;<br>      &lt;state&gt;MI&lt;/state&gt;<br>      &lt;zip&gt;94608&lt;/zip&gt;<br>      &lt;phone&gt;5105551309&lt;/phone&gt;<br>      &lt;fax&gt;5105554209&lt;/fax&gt;<br>   &lt;/dtdexternal_row&gt;</PRE></p>
<A NAME="TI8698"></A><p>The DTD is in <i>dtdexternal.dtd</i>:<p><PRE> &lt;?xml version="1.0" encoding="UTF-16LE"?&gt;&lt;!ELEMENT dtdexternal (dtdexternal_row*)&gt;<br>&lt;!ELEMENT dtdexternal_row (id, last_name, first_name, title, street, city, state, zip, phone, fax)&gt;<br>&lt;!ELEMENT id (#PCDATA)&gt;<br>&lt;!ELEMENT last_name (#PCDATA)&gt;<br>&lt;!ELEMENT first_name (#PCDATA)&gt;<br>&lt;!ELEMENT title (#PCDATA)&gt;<br>&lt;!ELEMENT street (#PCDATA)&gt;<br>&lt;!ELEMENT city (#PCDATA)&gt;<br>&lt;!ELEMENT state (#PCDATA)&gt;<br>&lt;!ELEMENT zip (#PCDATA)&gt;<br>&lt;!ELEMENT phone (#PCDATA)&gt;<br>&lt;!ELEMENT fax (#PCDATA)&gt;</PRE></p>
<p><img src="images/note.gif" width=17 height=17 border=0 align="bottom" alt="Note"> <span class=shaded>MetaDataExternal! not supported for dot notation</span> <A NAME="TI8699"></A>The metadata cannot be saved in an external file if you use
dot notation to generate the XML.</p>
<A NAME="CIHHCBHD"></A><h4>Associating a namespace
with an exported schema</h4>
<A NAME="TI8700"></A><p>If
you export metadata in the form of a schema, you can associate a
namespace with the schema. To do so, right-click the root element
in the Export/Import template view and select Schema Options
from the pop-up menu. In the dialog box, specify the namespace prefix
and URI. </p>
<A NAME="TI8701"></A><p>When the Meta Data Type property is XMLSchema! and the Save
Meta Data property is MetaDataInternal!, so that the XML schema
is generated inline, you can specify a name for the root element.
If the root element name is specified, it appears in the generated
XML. </p>
<A NAME="TI8702"></A><p>In the following example, the root element name is <b>Contacts</b>,
the namespace prefix is <b>po</b>, and the URI is <i>http://www.example.com/PO1</i>. </p>
<A NAME="TI8703"></A><p>The example shows the header and the first row of the generated
XML:<p><PRE> &lt;?xml version="1.0" encoding="UTF-16LE" standalone="no"?&gt;<br>&lt;Contacts&gt;<br>  &lt;xs:schema xmlns:po="http://www.example.com/PO1"<br>      xmlns:xs="http://www.w3.org/2001/XMLSchema"<br>      targetNamespace="http://www.example.com/PO1"<br>      elementFormDefault="qualified"<br>      attributeFormDefault="unqualified"&gt;<br>    &lt;xs:element name="d_contact_list"&gt;<br>      &lt;xs:complexType&gt;<br>        &lt;xs:sequence&gt;<br>          &lt;xs:element ref="d_contact_list_row"<br>              maxOccurs="unbounded" minOccurs="0"/&gt;<br>        &lt;/xs:sequence&gt;<br>      &lt;/xs:complexType&gt;<br>    &lt;/xs:element&gt;<br>    &lt;xs:element name="d_contact_list_row"&gt;<br>      &lt;xs:complexType&gt;<br>        &lt;xs:sequence&gt;<br>          &lt;xs:element ref="id"/&gt;<br>          &lt;xs:element ref="last_name"/&gt;<br>          &lt;xs:element ref="first_name"/&gt;<br>          &lt;xs:element ref="city"/&gt;<br>          &lt;xs:element ref="state"/&gt;<br>        &lt;/xs:sequence&gt;<br>      &lt;/xs:complexType&gt;<br>    &lt;/xs:element&gt;<br>    &lt;xs:element name="id" type="xs:int"/&gt;<br>    &lt;xs:element name="last_name" type="xs:string"/&gt;<br>    &lt;xs:element name="first_name" type="xs:string"/&gt;<br>    &lt;xs:element name="city" type="xs:string"/&gt;<br>    &lt;xs:element name="state" type="xs:string"/&gt;<br>  &lt;/xs:schema&gt;<br>  &lt;po:d_contact_list xmlns:po=<br>        "http://www.example.com/PO1" xmlns:xsi=<br>        "http://www.w3.org/2001/XMLSchema-instance"&gt;<br>    &lt;po:d_contact_list_row&gt;<br>      &lt;po:id&gt;1&lt;/po:id&gt;<br>      &lt;po:last_name&gt;Hildebrand&lt;/po:last_name&gt;<br>      &lt;po:first_name&gt;Jane&lt;/po:first_name&gt;<br>      &lt;po:city&gt;Emeryville&lt;/po:city&gt;<br>      &lt;po:state&gt;MI&lt;/po:state&gt;<br>    &lt;/po:d_contact_list_row&gt;</PRE></p>
<A NAME="TI8704"></A><p>By default, the generated XML is not associated with a namespace. </p>
<A NAME="CIHJJHCHK"></A><h2>Selecting templates at runtime</h2>
<A NAME="TI8705"></A><p>Two DataWindow properties, Export.XML.TemplateCount and Export.XML.Template[ ].Name,
enable you to provide a list of templates from which the user of
the application can select at runtime.</p>
<A NAME="TI8706"></A><p>The TemplateCount property gets the number of templates associated
with a DataWindow object. You can use this number as the upper limit
in a <b>FOR</b> loop that populates a drop-down list
with the template names. The <b>FOR</b> loop uses the
Template[ ].Name property.</p>
<A NAME="TI8707"></A><p><p><PRE> string ls_template_count, ls_template_name<br>long i<br> <br>ls_template_count=dw_1.Describe<br>  ("DataWindow.Export.XML.TemplateCount")<br> <br>for i=1 to Long(ls_template_count)<br>  ls_template_name=<br>    dw_1.Object.DataWindow.Export.XML.Template[i].Name<br>  ddlb_1.AddItem(ls_template_name)<br>next</PRE></p>
<A NAME="TI8708"></A><p>Before generating the XML, set the export template using the
text in the drop-down list box.</p>
<A NAME="TI8709"></A><p><p><PRE> dw_1.Object.DataWindow.Export.XML.UseTemplate=<br>   ddlb_1.text</PRE></p>

